/* A program to calculate the amount of free time a student has by using the timetable given by the user */

import tcdIO.*;

class LectureProgram {
  public static void main (String args[]) {
    Terminal terminal1; // used to store object representing the terminal
    Terminal terminal2;
    Lecture lec1;   // used to store object representing the first lecture
    Lecture lec2;
    Lecture lec3;
    Lecture lec4;
    Lecture lec5;

   //Terminal
    terminal1 = new Terminal("Lecture Timetable");

    terminal1.println("Enter the time of your Lectures below. Make sure that you enter them " +
    		"in order from earliest to latest.");
    
    terminal1.println("");
    terminal1.println("");
    
    //Enter Lecture 1 details here
    terminal1.println("Lecture 1:");
    lec1 = new Lecture (terminal1.readInt("Enter start time hour: "), terminal1.readInt("Enter start time minute: "), 
    terminal1.readInt("Enter end time hour: "), terminal1.readInt("Enter end time minute: "));
    
    		//Error if not correct time format, or end time is before start time
    while (lec1.getStarttimehour() > 24 || lec1.getStarttimeminute() > 59 || lec1.getEndtimehour() > 24 || lec1.getEndtimeminute() > 59
    		|| lec1.endtimeInMinute() <= lec1.starttimeInMinute()){
    	terminal1.println("");
    	terminal1.println("Error! Incorrect time submitted for Lecture 1! Try again..");
    	terminal1.println("");
    	lec1 = new Lecture(terminal1.readInt("Enter start time hour: "), terminal1.readInt("Enter start time minute: "), 
        		terminal1.readInt("Enter end time hour: "), terminal1.readInt("Enter end time minute: "));
    }
    terminal1.println("");
    
    //Enter Lecture 2 details here
    terminal1.println("Lecture 2:");
   lec2 = new Lecture(terminal1.readInt("Enter start time hour: "), terminal1.readInt("Enter start time minute: "), 
    		terminal1.readInt("Enter end time hour: "), terminal1.readInt("Enter end time minute: "));
   
   			//Error if not correct time format, or end time is before start time, or start time is before previous lecture end time
    while (lec2.getStarttimehour() > 24 || lec2.getStarttimeminute() > 59 || lec2.getEndtimehour() > 24 || lec2.getEndtimeminute() > 59 
    		|| lec2.starttimeInMinute() < lec1.endtimeInMinute() || lec2.endtimeInMinute() <= lec2.starttimeInMinute() ){
    	terminal1.println("");
    	terminal1.println("Error! Incorrect time submitted for Lecture 2! Try again..");
    	terminal1.println("");
    	lec2 = new Lecture(terminal1.readInt("Enter start time hour: "), terminal1.readInt("Enter start time minute: "), 
        		terminal1.readInt("Enter end time hour: "), terminal1.readInt("Enter end time minute: "));
    }
    terminal1.println("");
    
    //Enter Lecture 3 details here
    terminal1.println("Lecture 3:");
    lec3 = new Lecture (terminal1.readInt("Enter start time hour: "), terminal1.readInt("Enter start time minute: "), 
    terminal1.readInt("Enter end time hour: "), terminal1.readInt("Enter end time minute: "));
    
    		//Error if not correct time format, or end time is before start time, or start time is before previous lecture end time
    while (lec3.getStarttimehour() > 24 || lec3.getStarttimeminute() > 59 || lec3.getEndtimehour() > 24 || lec3.getEndtimeminute() > 59
    		|| lec3.starttimeInMinute() < lec2.endtimeInMinute() || lec3.endtimeInMinute() <= lec3.starttimeInMinute()){
    	terminal1.println("");
    	terminal1.println("Error! Incorrect time submitted for Lecture 3! Try again..");
    	terminal1.println("");
    	lec3 = new Lecture(terminal1.readInt("Enter start time hour: "), terminal1.readInt("Enter start time minute: "), 
        		terminal1.readInt("Enter end time hour: "), terminal1.readInt("Enter end time minute: "));
    }
    terminal1.println("");
    
    //Enter Lecture 4 details
    terminal1.println("Lecture 4:");
   lec4 = new Lecture(terminal1.readInt("Enter start time hour: "), terminal1.readInt("Enter start time minute: "), 
    		terminal1.readInt("Enter end time hour: "), terminal1.readInt("Enter end time minute: "));
    
   			//Error if not correct time format, or end time is before start time, or start time is before previous lecture end time
    while (lec4.getStarttimehour() > 24 || lec4.getStarttimeminute() > 59 || lec4.getEndtimehour() > 24 || lec4.getEndtimeminute() > 59
    		|| lec4.starttimeInMinute() < lec3.endtimeInMinute() || lec4.endtimeInMinute() <= lec4.starttimeInMinute()){
    	terminal1.println("");
    	terminal1.println("Error! Incorrect time submitted for Lecture 4! Try again..");
    	terminal1.println("");
    	lec4 = new Lecture(terminal1.readInt("Enter start time hour: "), terminal1.readInt("Enter start time minute: "), 
        		terminal1.readInt("Enter end time hour: "), terminal1.readInt("Enter end time minute: "));
    }
    
    terminal1.println("");

	//Enter Lecture 5 details
    terminal1.println("Lecture 5:");
    lec5 = new Lecture (terminal1.readInt("Enter start time hour: "), terminal1.readInt("Enter start time minute: "), 
    terminal1.readInt("Enter end time hour: "), terminal1.readInt("Enter end time minute: "));
    
    		//Error if not correct time format, or end time is before start time, or start time is before previous lecture end time
    while (lec5.getStarttimehour() > 24 || lec5.getStarttimeminute() > 59 || lec5.getEndtimehour() > 24 || lec5.getEndtimeminute() > 59
    		|| lec5.starttimeInMinute() < lec4.endtimeInMinute()|| lec5.endtimeInMinute() <= lec5.starttimeInMinute()){
    	terminal1.println("");
    	terminal1.println("Error! Incorrect time submitted for Lecture 5! Try again..");
    	terminal1.println("");
    	lec5 = new Lecture(terminal1.readInt("Enter start time hour: "), terminal1.readInt("Enter start time minute: "), 
        		terminal1.readInt("Enter end time hour: "), terminal1.readInt("Enter end time minute: "));
    }

    terminal1.println("");
    
    //Total lecture time:
    
    terminal1.println("Your total lecture time is: " + (lec1.calculateLecturelength()+lec2.calculateLecturelength()+
    		lec3.calculateLecturelength()+lec4.calculateLecturelength() +lec5.calculateLecturelength() )/60 + " hours"); 
    
    // Free time
     
    terminal1.println("Your total free time is: " + (1440 - (lec1.calculateLecturelength()+lec2.calculateLecturelength()+
    		lec3.calculateLecturelength()+lec4.calculateLecturelength() +lec5.calculateLecturelength() ))/60 + " hours"); 
    terminal1.println("");
    if ((1440 - (lec1.calculateLecturelength()+lec2.calculateLecturelength()+
    		lec3.calculateLecturelength()+lec4.calculateLecturelength() +lec5.calculateLecturelength() )) < 0){
    	terminal1.println("Error! Lectures do no fit in 24 hours");
    }
    terminal1.println("See other window for timetable of free time");
    
    
    
    //Timetable of free time:
    
    terminal2 = new Terminal ("Timetable of Free time");
    terminal2.println("Your timetable of free time:");
    terminal2.println("");
    
    
    		//The timetable will only be printed if there is free time during the day
    if ((lec1.calculateLecturelength()+lec2.calculateLecturelength()+
    		lec3.calculateLecturelength()+lec4.calculateLecturelength() +lec5.calculateLecturelength() ) < 1440){
    	
    //Time between midnight and first lecture
    if (lec1.getStarttimehour() != 00){
    	
    	//check if "0" needs to be added
    if (lec1.getStarttimeminute() >= 0 & lec1.getStarttimeminute() <= 9){
    	terminal2.println("00:00 -  " + lec1.getStarttimehour() + ":" + "0" + lec1.getStarttimeminute());
    }
    else {
    	terminal2.println("00:00 - " + lec1.getStarttimehour() + ":" + lec1.getStarttimeminute());
    }
    }
    
    	//Time between lectures
    
    		//check that there's free time between lec 1 + 2
    if (!(lec2.getStarttimehourBetween(lec1) == 0 & lec2.getStarttimeminuteBetween(lec1) == 0)){
    		//check if "0" needs to be added for lec 1 end time
        if (lec1.getEndtimeminute() >= 0 & lec1.getEndtimeminute() <= 9){
        terminal2.print(lec1.getEndtimehour() + ":" + "0" + lec1.getEndtimeminute());
        }
        else {
        	terminal2.print(lec1.getEndtimehour() + ":" + lec1.getEndtimeminute());
        	//check if "0" needs to be added for lec 2 start time
        }
        if (lec2.getStarttimeminute() >=0 & lec2.getStarttimeminute() <= 9){
        terminal2.println(" - " + lec2.getStarttimehour() + ":" + "0" + lec2.getStarttimeminute());
        }
        else {
      terminal2.println(" - " + lec2.getStarttimehour() + ":" + lec2.getStarttimeminute());
        }
    }

    
    		//check that there's free time between lec 2 + 3
    if (!(lec3.getStarttimehourBetween(lec2) == 0 & lec3.getStarttimeminuteBetween(lec2) == 0)){
    		//check if "0" needs to be added for lec 2 end time
        if (lec2.getEndtimeminute() >= 0 & lec2.getEndtimeminute() <= 9){
        terminal2.print(lec2.getEndtimehour() + ":" + "0" + lec2.getEndtimeminute());
        }
        else {
        	terminal2.print(lec2.getEndtimehour() + ":" + lec2.getEndtimeminute());
        }
        	//check if "0" needs to be added for lec 3 start time
        if (lec3.getStarttimeminute() >=0 & lec3.getStarttimeminute() <= 9){
        terminal2.println(" - " + lec3.getStarttimehour() + ":" + "0" + lec3.getStarttimeminute());
        }
        else {
      terminal2.println(" - " + lec3.getStarttimehour() + ":" + lec3.getStarttimeminute());
        }
    }
    
    		//check that there's free time between lec 3 + 4
    if (!(lec4.getStarttimehourBetween(lec3) == 0 & lec4.getStarttimeminuteBetween(lec3) == 0)){
    		//check if "0" needs to be added to lec 3 end time
        if (lec3.getEndtimeminute() >= 0 & lec3.getEndtimeminute() <= 9){
        terminal2.print(lec3.getEndtimehour() + ":" + "0" + lec3.getEndtimeminute());
        }
        else {
        	terminal2.print(lec3.getEndtimehour() + ":" + lec3.getEndtimeminute());
        }
        	//check if "0" needs to be added to lec 4 start time
        if (lec4.getStarttimeminute() >=0 & lec4.getStarttimeminute() <= 9){
        terminal2.println(" - " + lec4.getStarttimehour() + ":" + "0" + lec4.getStarttimeminute());
        }
        else {
      terminal2.println(" - " + lec4.getStarttimehour() + ":" + lec4.getStarttimeminute());
        }
        }

    		//check that there's free time between lec 4 + 5
    if (!(lec5.getStarttimehourBetween(lec4) == 0 & lec5.getStarttimeminuteBetween(lec4) == 0)){
    		//check if "0" needs to be added to lec 4 end time
    if (lec4.getEndtimeminute() >= 0 & lec4.getEndtimeminute() <= 9){
    terminal2.print(lec4.getEndtimehour() + ":" + "0" + lec4.getEndtimeminute());
    }
    else {
    	terminal2.print(lec4.getEndtimehour() + ":" + lec4.getEndtimeminute());
    		//check if "0" needs to be added to lec 5 start time
    }
    if (lec5.getStarttimeminute() >=0 & lec5.getStarttimeminute() <= 9){
    terminal2.println(" - " + lec5.getStarttimehour() + ":" + "0" + lec5.getStarttimeminute());
    }
    else {
  terminal2.println(" - " + lec5.getStarttimehour() + ":" + lec5.getStarttimeminute());
    }
    }
    
    		//check if there's free time between last lecture and end of day
    if (!((24 - lec5.getEndtimehour()) == 0 & (0 - lec5.getEndtimeminute()) == 0)){
    			//check if 0 needs to be added to lec 5 end time
    	if (lec5.getEndtimeminute() >= 0 & lec5.getEndtimeminute() <= 9){	
    		terminal2.println(lec5.getEndtimehour() + ":" + "0" + lec5.getEndtimeminute()+ " - " + "24:00" );
    	}
    	else {
    	 terminal2.println(lec5.getEndtimehour() + ":" + lec5.getEndtimeminute() + " - " + "24:00"); 
    }
    }
    }
    else {
    	terminal2.println("You have no free time!");		//If there is no free time to show in terminal2, then this string will be printed.
    }
}
}